package com.zeratul.demo.controller;

import com.zeratul.common.core.result.R;
import com.zeratul.demo.domain.User;
import com.zeratul.demo.service.DemoServiceImpl;
import com.zeratul.web.validate.utils.ZtValidUtils;
import jakarta.validation.Valid;
import jakarta.validation.Validator;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/demo")
@Slf4j
public class DemoController {

    @Autowired
    private DemoServiceImpl demoService;

    /**
     * 手动校验器
     */
    @Autowired
    private Validator validator;

    /**
     * 测试get请求
     * @return
     */
    @GetMapping("/get")
    public R<String> demo() {
        System.out.println("测试Get请求");
        log.debug("这是一条debug级别的日志！");
        log.info("这是一条info格式的日志！");
        demoService.demoService();
        log.warn("这是一条warn格式的日志！");
        log.error("这是一条error格式的日志！");
        return R.success("demo");
    }

    /**
     * 参数校验
     */
    @PostMapping("/v1/valid")
    public R<String> valids(@NotBlank(message = "账号不能为空") String username,
                            @NotBlank(message = "密码不能为空") String password){
        log.info("用户名：{}， 密码：{}", username, password);
        return R.success();
    }

    /**
     * 实体类校验
     */
    @PostMapping("/v2/valid")
    public R<String> valids2(@Valid User user){
        log.info("用户名：{}， 密码：{}", user.getUsername(), user.getPassword());
        return R.success();
    }

    /**
     * 请求体参数校验
     */
    @PostMapping("/v3/valid")
    public R<String> valids3(@RequestBody User user) {
        log.info("用户信息：{}", user);
        ZtValidUtils.validate(user);
        return R.success();
    }

    /**
     * 请求缓存
     */
    @GetMapping("/v1/cache")
    public R<String> cache(@RequestParam String key) {
        return R.success(demoService.getService(key));
    }

    /**
     * 删除缓存
     */
    @GetMapping("/v1/del/cache")
    public R<String> delCache(@RequestParam String key) {
        return R.success(demoService.updateService(key));
    }
}
